<?php

include_once '../commons/Conexion.php';
include_once '../model/Comentario.php';
include_once '../daos/UsuarioDao.php';
include_once '../daos/AvanceDao.php';

/**
 * Description of ComentarioDao
 *
 * @author User
 */
class ComentarioDao {

    private $tComentario = 't014_comentarios';

    function __construct() {
        
    }

    /* @var $oComen Comentario 
     * @result $oComen Comentario
     */

    public function saveComent($oComen = null) {

        $oComen->setId(0);
        try {
            $query = 'insert into %s (tx_comentario,
                                    fe_comentario,
                                    in_conforme,
                                    fk_usuario,
                                    fk_avance) 
                values ("%s","%s","%s",%d,%d);';

            $query = sprintf($query, $this->tComentario, $oComen->getComentario(), $oComen->getFecha(), $oComen->getConforme(), $oComen->getUsuario()->getId(), $oComen->getAvance()->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);

            $query = 'select l.co_id from %s l where l.tx_comentario = "%s" and fe_comentario = "%s" and fk_usuario = %d  and fk_avance = %d;';
            $query = sprintf($query, $this->tComentario, $oComen->getComentario(), $oComen->getFecha(), $oComen->getUsuario()->getId(), $oComen->getAvance()->getId());

            $result = $conn->query($query);

            while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                $oComen->setId($fila['co_id']);
            }

            $conn->close();
        } catch (Exception $e) {
            
        }

        return $oComen;
    }

    /*
     * @var $lId long
     * @result $oComen Comentario
     */

    public function getComentById($lId = 0) {

        $oComen = null;

        try {
            $query = 'select l.* from %s l where l.co_id = %d order by l.co_id;';
            $query = sprintf($query, $this->tComentario, $lId);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            if (isset($result)) {
                $uDao = new UsuarioDao();
                $aDao = new AvanceDao();
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oComen = new Comentario();
                    $oComen->setId($fila['co_id']);
                    $oComen->setComentario($fila['tx_comentario']);
                    $oComen->setFecha($fila['fe_comentario']);
                    $oComen->setConforme($fila['in_conforme']);
                    $oComen->setAvance($aDao->getAvanceById($fila['fk_avance']));
                    $oComen->setUsuario($uDao->getUsuarioById($fila['fk_usuario']));
                }
            }

            $conn->close();
        } catch (Exception $e) {
            
        }
        return $oComen;
    }

    /*
     * @return $listComentarios List<Comentario>
     */

    public function getComentarios() {

        $listComentarios = null;

        try {
            $query = 'select l.* from %s l order by l.co_id';
            $query = sprintf($query, $this->tComentario);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            $listComentarios = array();
            $i = 0;
            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oComen = new Comentario();
                    $oComen->setId($fila['co_id']);
                    $oComen->setComentario($fila['tx_comentario']);
                    $oComen->setConforme($fila['in_conforme']);
                    $oComen->setFecha($fila['fe_comentario']);
                    $listComentarios[$i++] = $oComen;
                }
            }
            $conn->close();
        } catch (Exception $e) {
            
        }

        return $listComentarios;
    }

    /*
     * @var $lAvance long
     * @return $listComentarios List<Comentario>
     */

    public function getComentariosByAvance($lAvance) {

        $listComentarios = null;

        try {
            $query = 'select l.* from %s l where l.fk_avance = %d order by l.co_id';
            $query = sprintf($query, $this->tComentario, $lAvance);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            $listComentarios = array();
            $i = 0;
            if (isset($result)) {
                $uDao = new UsuarioDao();
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $oComen = new Comentario();
                    $oComen->setId($fila['co_id']);
                    $oComen->setComentario($fila['tx_comentario']);
                    $oComen->setFecha($fila['fe_comentario']);
                    $oComen->setConforme($fila['in_conforme']);
                    $oComen->setUsuario($uDao->getUsuarioById($fila['fk_usuario']));
                    $listComentarios[$i++] = $oComen;
                }
            }
            $conn->close();
        } catch (Exception $e) {
            
        }

        return $listComentarios;
    }
    
    /*
     * @var $lAvance long
     * @return $conformes long
     */

    public function getEstatusByAvance($lAvance,$estatus) {

        $listComentarios = null;

        try {
            $query = 'select l.co_id from %s l where l.fk_avance = %d and l.in_conforme = "%s";';
            $query = sprintf($query, $this->tComentario, $lAvance,$estatus);

            $conn = new Conexion();
            $conn->open();

            $result = $conn->query($query);

            $listComentarios = array();
            $i = 0;
            if (isset($result)) {
                while ($fila = @mysql_fetch_array($result, MYSQL_ASSOC)) {
                    $i++;
                }
            }
            $conn->close();
        } catch (Exception $e) {
            
        }

        return $i;
    }

    /*
     * @var $oComent Comentario
     * @result $oComent Comentario
     */

    public function updateComentario($oComen) {

        try {
            $query = 'update %s l set 
                tx_comentario = "%s",
                fe_comentario = "%s",
                in_conforme = "%s",
                fk_usuario = %d,
                fk_avance = %d
                where  l.co_id = %d;';
            $query = sprintf($query, $this->tComentario, $oComen->getComentario(), $oComen->getFecha(), $oComen->getConforme(), $oComen->getUsuario()->getId(), $oComen->getAvance()->getId(), $oComen->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();
        } catch (Exception $e) {
            $oComen = null;
        }

        return $oComen;
    }

    /*
     * @var $oComen Comentario
     * @return $result boolean
     */

    public function deleteComentario($oComen) {

        $result = false;
        try {
            $query = 'delete from %s where co_id = %d';
            $query = sprintf($query, $this->tComentario, $oComen->getId());

            $conn = new Conexion();
            $conn->open();
            $conn->query($query);
            $conn->close();

            $result = true;
        } catch (Exception $e) {
            
        }

        return $result;
    }

}

?>
